/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginEmpleados.java
 *
 * Created on 23/05/2011, 01:21:59 PM
 */

package GUI;

import Interfaz.InterfazControl;
import Motor.Empleado;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Vane
 */
public class LoginEmpleados extends javax.swing.JFrame {

    /** Creates new form LoginEmpleados */
	private InterfazControl laIntefaz;

    public LoginEmpleados(InterfazControl laInterfaz) {
		this.laIntefaz=laInterfaz;
        initComponents();
		this.setLocationRelativeTo(null); // Cambiar icono de java en ventana
        this.setIconImage(new ImageIcon("src/imagenes/marketplus2.png").getImage());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTUsuario = new javax.swing.JTextField();
        jBAceptar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();
        jTClave = new javax.swing.JPasswordField();
        jCSeleccionar = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(390, 350));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 3, 18));
        jLabel1.setText("Inicio de Sesión");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 0, 160, 25);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Usuario:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(90, 100, 60, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Clave:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(90, 130, 70, 20);

        jTUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(jTUsuario);
        jTUsuario.setBounds(170, 100, 190, 20);

        jBAceptar.setFont(new java.awt.Font("Tahoma", 1, 11));
        jBAceptar.setText("Aceptar");
        jBAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(jBAceptar);
        jBAceptar.setBounds(60, 210, 120, 30);

        jBCancelar.setFont(new java.awt.Font("Tahoma", 1, 11));
        jBCancelar.setText("Ir a Pág Principal");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(jBCancelar);
        jBCancelar.setBounds(190, 210, 140, 30);
        getContentPane().add(jTClave);
        jTClave.setBounds(170, 130, 190, 20);

        jCSeleccionar.setFont(new java.awt.Font("Tahoma", 1, 11));
        jCSeleccionar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elegir un Elemento...", "Cajera", "Gerente" }));
        jCSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCSeleccionarActionPerformed(evt);
            }
        });
        getContentPane().add(jCSeleccionar);
        jCSeleccionar.setBounds(170, 160, 190, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Tipo de Usuario:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(60, 160, 110, 20);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/9af3c91c766211f797d02ff38e79e50a.png"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(10, 10, 380, 310);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jTUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTUsuarioActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_jTUsuarioActionPerformed

	private void jBAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAceptarActionPerformed
		// TODO add your handling code here:
		String usuario = this.jTUsuario.getText();
        String contrasena = this.jTClave.getText();
        Integer indice = this.jCSeleccionar.getSelectedIndex();
		if ( jTUsuario.getText().isEmpty() || jTClave.getText().isEmpty() )
		{
			JOptionPane.showMessageDialog(null, "Por favor llene todos los campos", "Error al Registrar", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			if (indice == 0)
			{
				JOptionPane.showMessageDialog(null, "Error, debe escoger un tipo de usuario!");
			}
			else 
			{
				if (indice ==1)
				{
					Empleado empleadoCajera = this.laIntefaz.loginEmpleado(usuario, contrasena);
					String rol = "Cajera";
					if (empleadoCajera == null)
					{
						JOptionPane.showMessageDialog(null, "Usuario y/o contraseña invalido(s)");
					}
					if (empleadoCajera != null &&  empleadoCajera.getRol().equals (rol) )
					{
						this.dispose();
						
						CajeraRegistro irCajeraRegistro = new CajeraRegistro(laIntefaz,empleadoCajera);
						irCajeraRegistro.setVisible(true);
					}
					if (empleadoCajera != null && !empleadoCajera.getRol().equals (rol))
						JOptionPane.showMessageDialog(null, "Rol No Valido");
				}
				else
				{
					if (indice==2)
					{
						Empleado empleadoGerente=this.laIntefaz.loginEmpleado(usuario,contrasena);
						String rol = "Gerente";
						if (empleadoGerente == null)
						{
							JOptionPane.showMessageDialog(null, "Usuario y/o contraseña invalido(s)");
						}
						if (empleadoGerente != null && empleadoGerente.getRol().equals(rol))
						{
							this.dispose();
							Gerente iraGerente = new Gerente(laIntefaz,empleadoGerente);
							iraGerente.setVisible(true);
						}
						if (empleadoGerente != null && !empleadoGerente.getRol().equals(rol))
							JOptionPane.showMessageDialog(null, "Rol No Valido");						
					}
				}
			}
		}
	}//GEN-LAST:event_jBAceptarActionPerformed

	private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
		// TODO add your handling code here:
		Principal iraPrincipal=new Principal(laIntefaz);
		iraPrincipal.setVisible(true);
		this.dispose();
}//GEN-LAST:event_jBCancelarActionPerformed

	private void jCSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCSeleccionarActionPerformed
		// TODO add your handling code here:

	}//GEN-LAST:event_jCSeleccionarActionPerformed

    /**
    * @param args the command line arguments
    */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAceptar;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JComboBox jCSeleccionar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField jTClave;
    private javax.swing.JTextField jTUsuario;
    // End of variables declaration//GEN-END:variables

}
